class IssueHook < Redmine::Hook::Listener
  def controller_issues_edit_before_save(context)
    issue = context[:issue]
    params = context[:params]
    flg = 0

# Change next status
    if params[:change_status] == "next"
      issue.new_statuses_allowed_to(User.current).each do |s|
        if flg == 1
          issue.status_id = s.id
          break
        elsif flg == 0 && s.id == issue.status_id
          flg = 1
        end
      end
    end

# input approval field
    approval_field = ["",9,1,3,"",5,7]
    approver_field = ["",10,2,4,"",6,8]
    
    issue.custom_field_values.each do |c|
      if c.custom_field_id == approval_field[issue.status_id]
        if c.value == nil or c.value == ""
          c.value = Date::today.to_s
        end
      elsif c.custom_field_id == approver_field[issue.status_id]
        if c.value == nil or c.value == ""
          c.value = User.current.id.to_s
        end
      end
    end
  end
end
